package user_information;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.json.JSONArray;
import org.json.JSONObject;

import DB.DB_connection_pool;

public class User_point_record extends HttpServlet {
	private static final long serialVersionUID = 1L;

	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doPost(request,response);
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		PrintWriter out = response.getWriter();
		String user_account = request.getSession().getAttribute("user_account").toString();
		
		String select_sql = "select * from point_record where user_account = '"+user_account+"'";
		JSONObject jsonObj_r = new JSONObject();
		try {
			Connection conn = DB_connection_pool.data_pool.getConnection();
			PreparedStatement ps_select= conn.prepareStatement(select_sql);
			ResultSet rs = ps_select.executeQuery();
			JSONArray record_Array = new JSONArray();
			while(rs.next()){
				JSONObject jsonObj = new JSONObject();
				jsonObj.put("point_record_id", rs.getString("point_record_id"));
				jsonObj.put("point_record_time", rs.getDate("point_record_time"));
				jsonObj.put("point_record_event", rs.getString("point_record_event"));
				jsonObj.put("point_lostget", rs.getDouble("point_lostget"));
				
				record_Array.put(jsonObj);
			}
			
			rs.close();
			ps_select.close();
			conn.close();
			
			jsonObj_r.put("result", true);
			jsonObj_r.put("record_Array", record_Array);
		} catch (SQLException e) {
			jsonObj_r.put("result", false);
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		out.print(jsonObj_r.toString());
	}

}
